package controllers;

import models.cms.Post;
import utils.SessionUtils;

import java.util.List;

/**
 * Created by nileb on 2015/11/15.
 */
public class F extends SController {


    public static void view_post(long id) {
        Post post = Post.findById(id);
        render(post);
    }



    public static void new_post() {
        render("F/new_post.html");
    }

    public static void edit_post(long id) {
        Post post = Post.findById(id);
        render("F/new_post.html", post);
    }






    public static void del_post(long id) {
        Post post = Post.findById(id);
        post.state = Post.STATE_DELETED;
        post.save();
    }



    public static void my_drafts() {
        List<Post> posts = Post.find("user = ? and state = ? and state != 9 order by created_at desc", SessionUtils.getCurrentUser(), Post.STATE_DRAFT).fetch(20);
        render("F/list_posts.html", posts);
    }

    public static void my_favors() {
        List<Post> posts = Post.find("select p from models.cms.Post p where exists (select f from models.cms.Favor f where f.post = p and f.user = ?)", SessionUtils.getCurrentUser()).fetch(20);
        render("F/list_posts.html", posts);
    }

    public static void my_follows() {
        List<Post> posts = Post.find("select p from models.cms.Post p where exists (select f from models.cms.Follow f where f.post = p and f.user = ?)", SessionUtils.getCurrentUser()).fetch(20);
        render("F/list_posts.html", posts);
    }

    public static void reg() {
        render();
    }

    public static void login() {
        render("F/login.html");
    }

    public static void logout() {
        SessionUtils.clear();
    }


}
